package com.bosch.in.rtp1.ept.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bosch.in.rtp1.ept.action.form.DurationColorConditionForm;
import com.bosch.in.rtp1.ept.manager.DurationColorConManager;
import com.bosch.in.rtp1.ept.model.dto.DurationColorCon;
import com.bosch.in.rtp1.ept.util.ManagerFactory;


public class DurationColorConditionAction extends MappingDispatchAction{
	
	private static final Logger LOGGER = Logger.getLogger(DurationColorConditionAction.class);
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	public ActionForward saveColorCondition(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException 
			 {
		
		LOGGER.debug("Begin saveColorCondition");
		
		// create duration color condition manager instance
		DurationColorConManager durColConManager = ManagerFactory.getInstance().getDurationColorConManager();
		
		DurationColorConditionForm durColForm = (DurationColorConditionForm) form;
		
		// insert to database
		DurationColorCon durColCon = new DurationColorCon(durColForm.getRedDuration(), 
				durColForm.getYellowDuration(), durColForm.getRedDurationOp(), durColForm.getYellowDurationOp());
		
		durColConManager.updateColorCol(durColCon);
		
		LOGGER.debug("End saveColorCondition");
		
        return mapping.findForward("success");
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	public ActionForward loadColorCondition(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException 
			 {
		LOGGER.debug("Begin loadColorCondition");
		
		// create duration color condition manager instance
		DurationColorConManager durColConManager = ManagerFactory.getInstance().getDurationColorConManager();
		
		// load durationColorCondition conditions
		DurationColorCon durColCon = durColConManager.getDuratonColCon();
		
		// push loaded values to form
		((DurationColorConditionForm)form).setRedDuration(durColCon.getRedColVal());
		((DurationColorConditionForm)form).setRedDurationOp(durColCon.getRedColOp());
		((DurationColorConditionForm)form).setYellowDuration(durColCon.getYellowColVal());
		((DurationColorConditionForm)form).setYellowDurationOp(durColCon.getYellowColOp());
		
		LOGGER.debug("End loadColorCondition");
        return mapping.findForward("success");
	}
}
